layui.use(['form','jquery','jquery_cookie'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        $ = layui.jquery_cookie($);


    /**
     * 登录功能
     * 表单提交
     */
// 点击登录按钮时
    form.on('submit(login)', function (data){

        var uname = data.field.username;
        var pwd = data.field.password;

        $.ajax({
            type:'post',
            url: ctx+"/user/login",
            data: {
                username: uname,
                password: pwd
            },
            success: function (data){
                console.log(data);
                if (data.code==200){
                    // console.log("登录成功");
                    layer.msg("登陆成功", function (){
                        // 如果记住我被选中，就存一下cookie
                        if ($('#rememberMe').prop("checked")){
                            $.cookie("id",data.result.id, {expires:7});
                            $.cookie("username",data.result.username, {expires:7});
                            // $.cookie("user",data.result);
                        }else {
                            $.cookie("id",data.result.id);
                            $.cookie("username",data.result.username);
                        }

                        // 登录成功后，跳转到首页
                        window.location.href = ctx + "/main";
                    });
                }else {
                    console.log("登录失败")
                }
            }

        });

        // 不让表单提交
        return false;
    });




});
